package com.example.s.Activity.team;

import androidx.lifecycle.Observer;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.ViewTreeObserver;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.example.s.Activity.BaseActivity;
import com.example.s.R;
import com.example.s.adapter.InnerHolder;
import com.example.s.adapter.MyRecyclerViewAdapter;
import com.example.s.bean.TeamItem;
import com.example.s.bean.TeamMember;
import com.example.s.bean.TeamMessage;
import com.example.s.utils.LiveDataBus;
import com.example.s.utils.MyUtils;
import com.youth.banner.Banner;
import com.youth.banner.adapter.BannerImageAdapter;
import com.youth.banner.holder.BannerImageHolder;
import com.youth.banner.indicator.CircleIndicator;

import java.util.ArrayList;
import java.util.List;

public class TeamDetailActivity extends BaseActivity {
    private ImageView back;
    private Banner banner;
    private TextView summary;
    private TextView releaseTime;
    private TextView goOffTime;
    private TextView goOffPlace;
    private TextView destination;
    private TextView attention;
    private ImageView moreMember;
    private TextView allMemberNums;
    private RecyclerView teamMemberRv;
    private FrameLayout allContainer;


    private RecyclerView teamMessageRv;
    private TextView tvMessage;
    private Button enroll;
    private List<TeamMember> mMemberPhotoDataList = new ArrayList<>();
    private MyRecyclerViewAdapter<TeamMember> mMemberPhotoAdapter;
    private List<TeamMessage> mTeamMessageList = new ArrayList<>();
    private MyRecyclerViewAdapter<TeamMessage> mTeamMessageAdapter;

    private List<Integer> mBannerData = new ArrayList<>();
    private LinearLayout sendCommentContainer;
    private EditText etComment;
    private TextView send;
    private BannerImageAdapter bannerAdapter;


    private void initView() {

        allContainer = (FrameLayout) findViewById(R.id.allContainer);
        back = (ImageView) findViewById(R.id.back);
        banner = (Banner) findViewById(R.id.banner);
        summary = (TextView) findViewById(R.id.summary);
        releaseTime = (TextView) findViewById(R.id.releaseTime);
        goOffTime = (TextView) findViewById(R.id.goOffTime);
        goOffPlace = (TextView) findViewById(R.id.goOffPlace);
        destination = (TextView) findViewById(R.id.destination);
        attention = (TextView) findViewById(R.id.attention);
        moreMember = (ImageView) findViewById(R.id.moreMember);
        allMemberNums = (TextView) findViewById(R.id.allMemberNums);
        teamMemberRv = (RecyclerView) findViewById(R.id.teamMemberRv);
        teamMessageRv = (RecyclerView) findViewById(R.id.teamMessageRv);

        tvMessage = (TextView) findViewById(R.id.tvMessage);

        enroll = (Button) findViewById(R.id.enroll);
        sendCommentContainer = (LinearLayout) findViewById(R.id.sendCommentContainer);
        etComment = (EditText) findViewById(R.id.etComment);
        send = (TextView) findViewById(R.id.send);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_team_detail);
        initView();
        LiveDataBus.getInstance().with("currentTeam", TeamItem.class).observe(this, new Observer<TeamItem>() {
            @Override
            public void onChanged(TeamItem teamItem) {
                mBannerData.add(teamItem.getFakeConver());
                bannerAdapter.notifyDataSetChanged();
                goOffPlace.setText("西一门口");
                releaseTime.setText("2020/1/3");
                goOffTime.setText("2020/12/31 9:00");
                destination.setText("图书馆");
                attention.setText("本项目无需额外费用，请准时参加");

            }
        });
        back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        allMemberNums.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                LiveDataBus.getInstance().with("allTimeMember", List.class).setValue(mMemberPhotoDataList);
                startActivity(new Intent(TeamDetailActivity.this, TeamMemberActivity.class));
            }
        });
        MyUtils.getInstance().showEtContainerBySoftkey(findViewById(android.R.id.content), sendCommentContainer);
        tvMessage.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                MyUtils.getInstance().showEtContainerBySoftkey(findViewById(android.R.id.content), sendCommentContainer);
                etComment.requestFocus();  //获得焦点
                InputMethodManager imm = (InputMethodManager) etComment.getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
                imm.toggleSoftInput(0, InputMethodManager.HIDE_NOT_ALWAYS);//弹出小键盘
            }
        });
        allContainer.getViewTreeObserver().addOnScrollChangedListener(new ViewTreeObserver.OnScrollChangedListener() {
            @Override
            public void onScrollChanged() {
                Log.d(TAG, "onScrollChanged: ");
                if (sendCommentContainer.getVisibility() == View.VISIBLE) {
                    InputMethodManager imm = (InputMethodManager) etComment.getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
                    imm.hideSoftInputFromWindow(sendCommentContainer.getWindowToken(), 0);
                }
            }
        });
        setBanner();

        setMemberPhotoData();
        setMemberPhotoAdapter();

        setMessageData();
        setMessageAdapter();
    }


    private void setMessageData() {
        mTeamMessageList.add(new TeamMessage("小老板: ", "活动真不错"));
        mTeamMessageList.add(new TeamMessage("待放: ", "再约一波"));
    }

    private void setMessageAdapter() {
        teamMessageRv.setLayoutManager(new LinearLayoutManager(this) {
            @Override
            public boolean canScrollVertically() {
                return false;
            }
        });
        teamMessageRv.setAdapter(mTeamMessageAdapter = new MyRecyclerViewAdapter<TeamMessage>(mTeamMessageList, R.layout.rv_team_message_item) {
            @Override
            public void handleData(InnerHolder holder, TeamMessage item) {
                holder.setText(R.id.userName, item.getmUserName());
                holder.setText(R.id.message, item.getmMessage());
            }
        });
    }

    private void setMemberPhotoData() {
        mMemberPhotoDataList.add(new TeamMember(String.valueOf(R.drawable.yyw21), "队长", "木艺"));
        mMemberPhotoDataList.add(new TeamMember(String.valueOf(R.drawable.yyw22), "队员", "龙须"));
        mMemberPhotoDataList.add(new TeamMember(String.valueOf(R.drawable.yyw23), "队员", "春"));
        mMemberPhotoDataList.add(new TeamMember(String.valueOf(R.drawable.yyw24), "队员", "普通"));
        mMemberPhotoDataList.add(new TeamMember(String.valueOf(R.drawable.yyw25), "队员", "小老板"));
        allMemberNums.setText("共" + mMemberPhotoDataList.size() + "人");
    }

    private void setMemberPhotoAdapter() {
        teamMemberRv.setLayoutManager(new LinearLayoutManager(this, RecyclerView.HORIZONTAL, false));
        teamMemberRv.setAdapter(mMemberPhotoAdapter = new MyRecyclerViewAdapter<TeamMember>(mMemberPhotoDataList, R.layout.rv_team_member_headphoto_item) {
            @Override
            public void handleData(InnerHolder holder, TeamMember item) {
                holder.setSrcResource(R.id.headPhoto, Integer.parseInt(item.getHeadPhoto()));
            }


        });
    }

    private void setBanner() {
        banner.setAdapter(bannerAdapter = new BannerImageAdapter<Integer>(mBannerData) {
            @Override
            public void onBindView(BannerImageHolder holder, Integer data, int position, int size) {
                holder.imageView.setBackgroundResource(data);
            }
        })
                .addBannerLifecycleObserver(this)
                .setIndicator(new CircleIndicator(this))
                .setLoopTime(5000);
    }

}
